local skel = fk.CreateSkill {
  name = "emo__chujue",
}

Fk:loadTranslationTable{
  ["emo__chujue"] = "处决",
  [":emo__chujue"] = "出牌阶段限一次，你可以指定一名其他角色，将你的手牌依次按以下顺序和规则对其使用：黑色基本牌：雷【杀】；红色装备牌：【火攻】；锦囊牌：【出其不意】；黑色装备牌：【决斗】。若此牌对其造成/未造成伤害，你下一张牌目标+1/重铸所有牌。",

  ["#emo__chujue"] = "处决：指定一名其他角色，将你所有手牌按规定顺序转化对其使用",
  ["@emo__chujue"] = "处决",
  ["#emo__chujue-extra"] = "处决：为 %arg 额外指定目标",

  ["$emo__chujue1"] = "进入战斗状态",
  ["$emo__chujue2"] = "愿人类荣耀长存",
}

skel:addEffect("active", {
  anim_type = "offensive",
  prompt = "#emo__chujue",
  card_num = 0,
  target_num = 1,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and player ~= to_select
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local exclude = {} -- 无法使用的牌将排除
    while player:isAlive() and to:isAlive() do
      local hand = table.filter(player.player_cards[Player.Hand], function (id)
        return not table.contains(exclude, id)
      end)
      if #hand == 0 then break end
      local name, cid
      cid = table.find(hand, function (id) return Fk:getCardById(id).type == Card.TypeBasic and Fk:getCardById(id).color == Card.Black end)
      if cid then
        name = "thunder__slash"
      end
      if not cid then
        cid = table.find(hand, function (id) return Fk:getCardById(id).type == Card.TypeEquip and Fk:getCardById(id).color == Card.Red end)
        if cid then
          name = "fire_attack"
        end
      end
      if not cid then
        cid = table.find(hand, function (id) return Fk:getCardById(id).type == Card.TypeTrick end)
        if cid then
          name = "unexpectation"
        end
      end
      if not cid then
        cid = table.find(hand, function (id) return Fk:getCardById(id).type == Card.TypeEquip and Fk:getCardById(id).color == Card.Black end)
        if cid then
          name = "duel"
        end
      end
      if not cid then break end
      local card = Fk:cloneCard(name)
      card.skillName = skel.name
      card:addSubcard(cid)
      if not player:canUseTo(card, to, {bypass_distances = true, bypass_times = true }) then
        table.insert(exclude, cid)
      else
        local use = UseCardData:new { from = player, tos = {to}, card = card, extraUse = true }
        room:useCard(use)
        if player.dead then break end
        if use.damageDealt and use.damageDealt[to] then
          room:addPlayerMark(player, "@emo__chujue")
        else
          local cards = player:getCardIds("he")
          if #cards > 0 then
            room:recastCard(cards, player, skel.name)
            room:delay(400)
          end
        end
      end
    end
  end,
})

skel:addEffect(fk.TargetSpecifying, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@emo__chujue") > 0 and
      data.card.type ~= Card.TypeEquip and data.card.sub_type ~= Card.SubtypeDelayedTrick
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local max = player:getMark("@emo__chujue")
    room:setPlayerMark(player, "@emo__chujue", 0)
    local targets = data:getExtraTargets({bypass_distances = true})
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = max,
      prompt = "#emo__chujue-extra:::"..data.card:toLogString(),
      skill_name = skel.name,
      cancelable = true,
    })
    if #tos == 0 then return end
    for _, to in ipairs(tos) do
      data:addTarget(to)
    end
  end,
})

return skel
